<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref, unref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { useI18n } from '@/hooks/web/useI18n'
import { getMenuListApi } from '@/api/menu'
import { ElTag } from 'element-plus'
import AddButtonPermission from './AddButtonPermission.vue'
import { BaseButton } from '@/components/Button'
import { cloneDeep } from 'lodash-es'

const { t } = useI18n()

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const handleClose = async (tag: any) => {
  const formData = await getFormData()
  // 删除对应的权限
  setValues({
    permissionList: formData?.permissionList?.filter((v: any) => v.value !== tag.value)
  })
}

const showDrawer = ref(false)

const formSchema = reactive<FormSchema[]>([
  {
    field: 'parentGuid',
    label: '父级菜单',
    component: 'TreeSelect',
    componentProps: {
      nodeKey: 'guid',
      props: {
        label: 'name',
        value: 'guid',
        children: 'children'
      },
      highlightCurrent: true,
      expandOnClickNode: false,
      checkStrictly: true,
      checkOnClickNode: true,
      clearable: true,
      on: {
        change: async (val: number) => {
          const formData = await getFormData()
          if (val && formData.type === 0) {
            setValues({
              component: '##'
            })
          } else if (!val && formData.type === 0) {
            setValues({
              component: '#'
            })
          } else if (formData.type === 1) {
            setValues({
              component: unref(cacheComponent) ?? ''
            })
          }
        }
      }
    },
    optionApi: async () => {
      const res = await getMenuListApi()
      return res.data || [] //.list
    }
  },
  {
    field: 'name',
    label: t('menu.menuName'),
    component: 'Input'
  },
  {
    field: 'englishName',
    label: t('menu.menuName'),
    component: 'Input'
  },
  {
    field: 'icon',
    label: t('menu.icon'),
    component: 'Input'
  },
  {
    field: 'menuPath',
    label: t('menu.path'),
    component: 'Input'
  },
  //   {
  //     field: 'Guid',
  //     label: t('menu.menuId'),
  //     component: 'Input'
  //   },
  {
    field: 'status',
    label: t('menu.status'),
    component: 'Select',
    componentProps: {
      options: [
        {
          label: t('userDemo.disable'),
          value: 0
        },
        {
          label: t('userDemo.enable'),
          value: 1
        }
      ]
    }
  },
  {
    field: 'buttons',
    label: '操作按钮',
    component: 'Select',
    componentProps: {
      multiple: true,
      options: [
        {
          label: '新增',
          value: '新增'
        },
        {
          label: '编辑',
          value: '编辑'
        },
        {
          label: '删除',
          value: '删除'
        },
        {
          label: '导入',
          value: '导入'
        },
        {
          label: '导出',
          value: '导出'
        }
      ]
    }
  },
  {
    field: 'menuLevel',
    label: t('menu.MenuOrder'),
    component: 'Input',
    hidden: true
  },
  {
    field: 'menuOrder',
    label: t('menu.MenuOrder'),
    component: 'Input'
  },
  {
    field: 'remark',
    label: t('menu.Remark'),
    component: 'Input'
  },
  //   {
  //     field: 'permissionList',
  //     label: t('menu.permission'),
  //     component: 'CheckboxGroup',
  //     colProps: {
  //       span: 24
  //     },
  //     formItemProps: {
  //       slots: {
  //         default: (data: any) => (
  //           <>
  //             {data?.permissionList?.map((v) => {
  //               return (
  //                 <ElTag class="mr-1" key={v.value} closable onClose={() => handleClose(v)}>
  //                   {v.label}
  //                 </ElTag>
  //               )
  //             })}
  //             <BaseButton type="primary" size="small" onClick={() => (showDrawer.value = true)}>
  //               添加权限
  //             </BaseButton>
  //           </>
  //         )
  //       }
  //     }
  //   },
  {
    field: 'meta.hidden',
    label: t('menu.hidden'),
    component: 'Switch'
  }
  //   {
  //     field: 'meta.alwaysShow',
  //     label: t('menu.alwaysShow'),
  //     component: 'Switch'
  //   },
  //   {
  //     field: 'meta.noCache',
  //     label: t('menu.noCache'),
  //     component: 'Switch'
  //   },
  //   {
  //     field: 'meta.breadcrumb',
  //     label: t('menu.breadcrumb'),
  //     component: 'Switch'
  //   },
  //   {
  //     field: 'meta.affix',
  //     label: t('menu.affix'),
  //     component: 'Switch'
  //   },
  //   {
  //     field: 'meta.noTagsView',
  //     label: t('menu.noTagsView'),
  //     component: 'Switch'
  //   },
  //   {
  //     field: 'meta.canTo',
  //     label: t('menu.canTo'),
  //     component: 'Switch'
  //   }
])

const rules = reactive({
  // component: [required()],
  icon: [required()],
  name: [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose, setSchema } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    return formData
  }
}

const cacheComponent = ref('')

watch(
  () => props.currentRow,
  (value) => {
    if (!value) return
    const currentRow = cloneDeep(value)
    // cacheComponent.value = currentRow.type === 1 ? currentRow.component : ''
    // if (currentRow.parentId === 0) {
    //   setSchema([
    //     {
    //       field: 'component',
    //       path: 'componentProps.disabled',
    //       value: true
    //     }
    //   ])
    // } else {
    //   setSchema([
    //     {
    //       field: 'component',
    //       path: 'componentProps.disabled',
    //       value: false
    //     }
    //   ])
    // }
    // if (currentRow.type === 1) {
    //   setSchema([
    //     {
    //       field: 'component',
    //       path: 'componentProps.disabled',
    //       value: false
    //     }
    //   ])
    // } else {
    //   setSchema([
    //     {
    //       field: 'component',
    //       path: 'componentProps.disabled',
    //       value: true
    //     }
    //   ])
    // }
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})

// const confirm = async (data: any) => {
//   const formData = await getFormData()
//   setValues({
//     permissionList: [...(formData?.permissionList || []), data]
//   })
// }
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" />
  <!-- <AddButtonPermission v-model="showDrawer" @confirm="confirm" /> -->
</template>
